﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Config.cs" company="TomCorp">
//   Thomas Heuberger
// </copyright>
// <summary>
//   The config.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace FHIntranetParser {
    using System.Linq;
    using System.Xml.Linq;

    /// <summary>
    /// The config.
    /// </summary>
    public class Config {
        /// <summary>
        /// The URI of the student's intranet portal.
        /// </summary>
        private readonly string uri;

        /// <summary>
        /// The id of the input element for the username.
        /// </summary>
        private readonly string usernameInput;

        /// <summary>
        /// The id of the input element for the password.
        /// </summary>
        private readonly string passwordInput;

        /// <summary>
        /// The username.
        /// </summary>
        private readonly string username;

        /// <summary>
        /// The password.
        /// </summary>
        private readonly string password;

        /// <summary>
        /// Initializes a new instance of the <see cref="Config"/> class.
        /// </summary>
        /// <param name="uri">
        /// The uri.
        /// </param>
        /// <param name="usernameInput">
        /// The username input.
        /// </param>
        /// <param name="passwordInput">
        /// The password input.
        /// </param>
        /// <param name="username">
        /// The username.
        /// </param>
        /// <param name="password">
        /// The password.
        /// </param>
        public Config(string uri, string usernameInput, string passwordInput, string username, string password) {
            this.uri = uri;
            this.usernameInput = usernameInput;
            this.passwordInput = passwordInput;
            this.username = username;
            this.password = password;
        }

        /// <summary>
        /// Gets the URI.
        /// </summary>
        public string URI {
            get { return this.uri; }
        }

        /// <summary>
        /// Gets the id of the input element for the username.
        /// </summary>
        public string UsernameInput {
            get { return this.usernameInput; }
        }

        /// <summary>
        /// Gets the id of the input element for the password.
        /// </summary>
        public string PasswordInput {
            get { return this.passwordInput; }
        }

        /// <summary>
        /// Gets the username.
        /// </summary>
        public string Username {
            get { return this.username; }
        }

        /// <summary>
        /// Gets the password.
        /// </summary>
        public string Password {
            get { return this.password; }
        }

        /// <summary>
        /// Loads the config from the xml.
        /// </summary>
        /// <param name="path">
        /// The path.
        /// </param>
        /// <returns>
        /// The <see cref="Config"/>.
        /// </returns>
        public static Config LoadFromXml(string path) {
            var configFile = XDocument.Load(path);
            return configFile.Descendants("FHIntranetParserConfig").Select(item =>
                 new Config(
                     item.Element("URI").Value,
                     item.Element("UsernameInput").Value,
                     item.Element("PasswordInput").Value,
                     item.Element("Credentials").Element("Username").Value,
                     item.Element("Credentials").Element("Password").Value)).FirstOrDefault();
        }
    }
}
